package com.settlement.system.controller;

import com.settlement.system.common.constant.StandardConstant;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.common.util.RequestContextUtils;
import com.settlement.system.common.util.SecurityUtils;
import com.settlement.system.common.util.common.FasUtil;
import com.settlement.system.common.util.common.SystemUserUtil;
import com.settlement.system.mapper.security.userdetails.SysUserDetails;
import com.settlement.system.model.entity.common.BasePager;
import com.settlement.system.model.entity.common.SinglePage;
import com.settlement.system.model.entity.common.SystemUser;
import com.settlement.system.model.entity.common.TblOrderAuditBill;
import com.settlement.system.model.vo.TblOrderAuditBillVo;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblOrderAuditBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单稽核单据主表 前端控制器
 *
 * @author 小船
 * @since 2020-10-21
 */

@Api(tags = "订单稽核单据主表", description = "订单稽核单据主表")
@RestController
@RequestMapping("/order/audit/bill")
@Slf4j
public class TblOrderAuditBillController extends ExcelController<TblOrderAuditBill> {

    @Resource
    private ITblOrderAuditBillService tblOrderAuditBillService;

    @Override
    public Class<?> getModelClass() {
        return TblOrderAuditBill.class;
    }

    @Override
    public IBaseService init() {
        return tblOrderAuditBillService;
    }

    @ApiOperation("单据分页查询--代办/已办/归档")
    @RequestMapping(value = "selectBilllist", method = {RequestMethod.POST})
    @ResponseBody
    protected ObjectDataResponse selectBilllist(HttpServletRequest request, @RequestBody BasePager<Map<String, Object>> model) {
        Map<String, Object> result = new HashMap<>(16);
        try {
            this.insetMenuMemory(model.getModel());
            setParamsModel(model.getModel());
            int count = tblOrderAuditBillService.selectBillCount(model.getModel());
            List<TblOrderAuditBill> rows = tblOrderAuditBillService.selectBillByPage(new SinglePage(model), model.getModel(),
                    model.getOrderByField(), model.getOrderBy());
            result.put("rows", rows);
            result.put("total", count);
            setFootModel(result, model.getModel());
        } catch (Exception ex) {
            log.error("获取列表出错", ex);
            return new ObjectDataResponse(500, "获取列表失败，请联系管理员！");
        }
        return new ObjectDataResponse<Map>(result, 200, "获取列表成功");
    }


    @ApiOperation("查询订单稽核单据明细信息")
    @RequestMapping(value = "selectBillDtl", method = {RequestMethod.GET})
    @ResponseBody
    public ObjectDataResponse selectBilllist(HttpServletRequest request, String billNo) {
        TblOrderAuditBillVo repVo = null;
        try {
            log.info("查询订单稽核单据明细信息开始，单号是{}", billNo);
            if (StringUtils.isEmpty(billNo)) {
                return new ObjectDataResponse(500, "传入单号不能为空！");
            }
            Map<String, Object> params = new HashMap<>(16);
            params.put("billNo", billNo);
            repVo = tblOrderAuditBillService.selectBillDtl(params);
        } catch (Exception ex) {
            log.error("获取列表出错", ex);
            return new ObjectDataResponse(500, "获取列表失败，请联系管理员！");
        }
        log.info("查询单据明细结算，单号是{}", billNo);
        return new ObjectDataResponse<TblOrderAuditBillVo>(repVo, 200, "获取列表成功");
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        SysUserDetails user = SecurityUtils.getUser();;
        model.put("authorityNo", "auth_001");
        model.put("loginName", user.getUsername());
        FasUtil.formatInQueryCondition(model, "refBillNo", "spOrderNo", "threeLevelChannelNo", "orderSourceNo", "orderBillStatusNo", "companySubject", "shopNo", "lossReasonNo");
        //model.put("followUpResponsibility",
        //      FollowUpResponsibilityEnum.getNoStringByName((String) model.get("followUpResponsibility")));
        model.put("orderSourcePermissionList", RequestContextUtils.getHttpSession().getAttribute(StandardConstant.USER_ORDER_SOURCE_PERMISSION));
    }

    @Override
    protected List<TblOrderAuditBill> getExportDate(Map<String, Object> params) throws ServiceException {
        List<TblOrderAuditBill> tempList = new ArrayList<>();
        tblOrderAuditBillService.getDataBillByHandler(params, new ResultHandler<TblOrderAuditBill>() {

            @Override
            public void handleResult(ResultContext<? extends TblOrderAuditBill> resultContext) {
                tempList.add(resultContext.getResultObject());
            }
        });
        return tempList;
    }

}

